import { Inject, Controller, Get, Post, Body, Query} from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { BookService } from '../../service/user/book.service';
import { Book } from '../../entity/user/book';
import { PetService } from '../../service/user/pet.service';
import * as fs from 'fs';
import * as path from 'path';

@Controller('/api/user')
export class APIController {
  @Inject()
  ctx: Context;

  @Inject()
  bookService: BookService;
  @Inject()
  petService: PetService;

  @Get('/book', { description: '获取预约表' })
  async getbook(@Query('ownerid') ownerid: string) {
    const result = await this.bookService.getbook(ownerid)
    if(result.length!=0){
      for(var i = 0 ;i< result.length; i++){
          var id = result[i].petid
          const petinfo = await this.petService.petinfo(id)
          const fileName = petinfo.petpic.imageurl;
          const petpic = fs.readFileSync(path.join(__dirname, '../../../public/upload', fileName));
          result[i]["src"] = petpic
          result[i]["petinfo"] = petinfo 
      }
  }
    return { success: true, message: '预约查询成功', result }
  }

  @Post('/book', { description: '预约' })
  async book(@Body() book: Book) {
    console.log(book);
    const result = await this.bookService.book(book)
    return { success: true, message: '预约提交成功', data: { result } }
  }

 
}